package com.yaolisheng.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static java.lang.System.getProperty;
import static java.lang.Character.isSpaceChar;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2021/5/5 12:16
 */
public class StringUtils {

	public static final String LINE = getProperty("line.separator");

	/**
	 * 判断是空字符串
	 */
	public static boolean isEmpty(String value) {
		return value == null || value.isEmpty();
	}

	/**
	 * 判断不是空字符串
	 */
	public static boolean isNotEmpty(String value) {
		return !isEmpty(value);
	}

	/**
	 * 去掉空格
	 */
	public static String trim(String value) {
		return isEmpty(value) ? value
				: isSpaceChar(value.charAt(0))
						|| isSpaceChar(value.length() - 1) ? value.trim()
								: value;
	}

	/**
	 * 将List拼装成以，号分割的字符串
	 */
	public static <T> String assembly(List<T> list, String limit) {
		return list == null || list.isEmpty() ? null
				: assembly(list.iterator(), limit);
	}

	/**
	 * 将Set拼装成以，号分割的字符串
	 */
	public static <T> String assembly(Set<T> set, String limit) {
		return set == null || set.isEmpty() ? null
				: assembly(set.iterator(), limit);
	}

	public static <T> String assembly(Iterator<T> it, String limit) {
		StringBuilder sb = new StringBuilder();
		while (it.hasNext()) {
			sb.append(it.next());
			sb = it.hasNext() ? sb.append(limit) : sb;
		}
		return sb.toString();
	}
}
